/*
 *  Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.material.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.MaterialUnitException;
import com.pitop.core.tools.StringTools;
import com.pitop.material.dao.MaterialUnitDAO;
import com.pitop.material.entity.MaterialUnit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("materialUnitSV")
@Slf4j
public class MaterialUnitSVImpl extends BaseSVImpl<MaterialUnit, Long> implements MaterialUnitSV {

    @Autowired
    private MaterialUnitDAO materialUnitDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return materialUnitDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(MaterialUnit entity) throws BaseException {
        entity.setCode(String.valueOf(uidGenerator.getUID()));
        super.save(entity);
    }

    /**
     * 加载一个对象MaterialUnit
     *
     * @param id * @param code
     * @return MaterialUnit
     */
    @Override
    public MaterialUnit load(Long id, String code) {
        if (id == null && code == null) {
            throw new MaterialUnitException(BaseException.BaseExceptionEnum.Illegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return materialUnitDAO.load(param);
    }

    /**
     * 加载一个对象MaterialUnit 通过id
     *
     * @param id
     * @return MaterialUnit
     */
    @Override
    public MaterialUnit loadById(Long id) {
        if (id == null) {
            throw new MaterialUnitException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return materialUnitDAO.loadById(id);
    }

    /**
     * 加载一个对象MaterialUnit 通过code
     *
     * @param code
     * @return MaterialUnit
     */
    @Override
    public MaterialUnit loadByCode(String code) {
        if (code == null) {
            throw new MaterialUnitException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return materialUnitDAO.loadByCode(code);
    }

    /**
     * 加载一个对象MaterialUnit 通过unit
     *
     * @param unit
     * @return MaterialUnit
     */
    @Override
    public MaterialUnit loadByUnit(String unit) {
        if (StringTools.isEmpty(unit)) {
            throw new MaterialUnitException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Map<String, Object> params = new HashedMap();
        params.put("unit", unit);
        return materialUnitDAO.load(params);
    }


    /**
     * 删除对象MaterialUnit
     *
     * @param id * @param code
     * @return MaterialUnit
     */
    @Override
    public void delete(Long id, String code) {
        if (id == null && code == null) {
            throw new MaterialUnitException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        materialUnitDAO.delete(param);
    }


    /**
     * 查询MaterialUnit分页
     *
     * @param materialUnit 对象
     * @param offset       查询开始行
     * @param limit        查询行数
     * @return List<MaterialUnit>
     */
    @Override
    public List<MaterialUnit> list(MaterialUnit materialUnit, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (materialUnit != null) {
            map = JSON.parseObject(JSON.toJSONString(materialUnit, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return materialUnitDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(MaterialUnit materialUnit) {
        Map<String, Object> map = null;
        if (materialUnit != null) {
            map = JSON.parseObject(JSON.toJSONString(materialUnit, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return materialUnitDAO.count(map);
    }

    /**
     * 查询MaterialUnit分页
     *
     * @param id     * @param code
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<MaterialUnit>
     */
    @Override
    public List<MaterialUnit> list(Long id, String code, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return materialUnitDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(Long id, String code) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return materialUnitDAO.count(param);
    }

}
